package com.lvyq.service.impl;

import com.lvyq.cache.CacheManager;
import com.lvyq.common.MsgTypeConstant;
import com.lvyq.common.api.APITypeConstant;
import com.lvyq.model.ai.Corpus;
import com.lvyq.model.apiinfo.ApiInfo;
import com.lvyq.model.receive.ReceiveMsg;
import com.lvyq.model.reply.ReplyMsg;
import com.lvyq.model.weater.WeatherItem;
import com.lvyq.repository.api.ApiInfoRepository;
import com.lvyq.service.ConvenientService;
import com.lvyq.util.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * @program: weg
 * @description: 便民服务
 * @author: lvyq
 * @create: 2023-03-23 17:54
 **/
@Service
public class ConvenientServiceImpl implements ConvenientService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private ApiInfoRepository apiInfoRepository;

    @Override
    public String orderReply(String replyContent, ReceiveMsg receiveMsg) throws IOException {
         //便民服务-天气查询
        if (receiveMsg.getContent().startsWith("天气查询#")){
          replyContent= weaterReply(replyContent,receiveMsg);
        }
        //快递查询
        if (receiveMsg.getContent().startsWith("快递查询#")){
            replyContent= expressReply(replyContent,receiveMsg);
        }

        //ChatGPT
        if (receiveMsg.getContent().startsWith("G#")){
            replyContent= chatGPTReply(replyContent,receiveMsg);
        }
       // System.out.println("replyContent"+replyContent);
        return replyContent;
    }

    /** 
    * @Description: chatGPT
     * @param replyContent
    * @param receiveMsg
    * @return: {@link java.lang.String}
    * @Author: lvyq
    * @Date: 2023/4/21 10:03
    */
    private String chatGPTReply(String replyContent, ReceiveMsg receiveMsg) {
        if (CacheManager.get(receiveMsg.getMsgId())!=null){
            String [] contents = receiveMsg.getContent().split("#");
            String question = contents[1];
            Query query= new Query().addCriteria(Criteria.where("question").is(question));
            List<Corpus> corpuses =mongoTemplate.find(query,Corpus.class);
            Random random = new Random();
            if (corpuses.size()>0){
                replyContent=corpuses.get(random.nextInt(corpuses.size())).getAnswer();
            }else {
                replyContent="生成答案中，由于限制无法主动推送相应答案，请稍后用相同指令重试!";
            }
        }else {
            CacheManager.set(receiveMsg.getMsgId(),receiveMsg.getMsgId());
            String [] contents = receiveMsg.getContent().split("#");
            ApiInfo apiInfo = apiInfoRepository.findByType(APITypeConstant.GPT);
            replyContent= TCGPTUtil.getAnswer(apiInfo,contents[1]);
            Corpus corpus = new Corpus();
            corpus.setAnswer(replyContent);
            corpus.setQuestion(contents[1]);
            mongoTemplate.save(corpus);

        }
        if (replyContent.length()>650){
            replyContent=replyContent.substring(0,650);
        }
        return replyContent;
    }

    /**
    * @Description: 快递查询
    * @param replyContent
    * @param receiveMsg
    * @return: {@link java.lang.String}
    * @Author: lvyq
    * @Date: 2023/4/17 16:40
    */

    private String expressReply(String replyContent, ReceiveMsg receiveMsg) {
        //获取快递编号
        String [] contents = receiveMsg.getContent().split("#");
        ApiInfo apiInfo = apiInfoRepository.findByType(APITypeConstant.KDCX);
        replyContent= ExpressQueryUtils.getExpressInfo(apiInfo,contents[1]);
        return replyContent;
    }

    /** 
    * @Description: 天气查询
    * @params:  * @param replyContent
     * @param receiveMsg
    * @return: 
    * @Author: lvyq
    * @Date: 2023/3/23 18:07
    */
    public String weaterReply(String replyContent, ReceiveMsg receiveMsg) throws IOException {
            String [] contents = receiveMsg.getContent().split("#");
            if (contents.length==2){
                String city=contents[1];

                if (city.equals("重庆")){
                    city= "chongqing";
                }else {
                    city= PinyinUtil.getPingYin(city);
                }
                ApiInfo apiInfo = apiInfoRepository.findByType(APITypeConstant.TQCX);
                WeatherItem weatherItem = WeatherUtils.getWeater(apiInfo,city);
                //进行替换
                replyContent=replyContent.replace("{weatherName}",weatherItem.getWeatherName())
                        .replace("{nowWeather}",weatherItem.getNowWeather())
                        .replace("{todayWeatherTQ}",weatherItem.getTodayWeatherTQ())
                        .replace("{todayWeatherWD}",weatherItem.getTodayWeatherWD())
                        .replace("{shidu}",weatherItem.getShidu() )
                        .replace("{fengxiang}",weatherItem.getFengxiang())
                        .replace("{ziwaixian}",weatherItem.getZiwaixian())
                        .replace("{kongqiTitle}",weatherItem.getKongqiTitle())
                        .replace("{pm}",weatherItem.getPm())
                        .replace("{kongqiOth}",weatherItem.getKongqiOth())
                        .replace("{time}",weatherItem.getTime());
            }else {
                replyContent="小窝不知道您查询哪个城市的天气呢";
            }
             return replyContent;
        }


}
